<template>
  <!-- ref属性  快速获取Dom元素 -->
  <div class="list" ref="wrapper">
    <div>
      <div class="area">
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button">{{this.currentCity}}</div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div class="button-wrapper"
            v-for="item of hot"
            :key="item.id"
            @click="handCity(item.name)">
            <div class="button">{{item.name}}</div>
          </div>
        </div>
      </div>
      <!-- 城市列表 -->
      <div class="area" v-for="(item, key) of cities" :key="key" :ref="key">
        <div class="title border-topbottom">{{key}}</div>
        <div class="item-list" v-for="innerItem of item" :key="innerItem.id" @click="handCity(innerItem.name)">
            <div class="item border-bottom">{{innerItem.name}}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState,mapMutations } from 'vuex'
import BScroll from 'better-scroll'
export default {
  name: 'Citylist',
  props: {
    hot: Array,
    cities: Object,
    letter: String // 接收父组件传递过来的letter参数
  },
  methods: {
    handCity (city) {
      // 调用Vuex的dispatch派发到actions,但是必须调用actions来修改store的数据
      // this.$store.dispatch('changeCity',city)
      // 直接调用Vuex commit 方法不需要经过actions来修改store的数据
      // this.$store.commit('changeCity', city)
      this.changeCity(city)
      this.$router.push('/') // 编程试路由
    },
    ...mapMutations(['changeCity'])
  },
  watch: {
    // 借助监听器 watch 来监听 letter 的变化
    letter () {
       // console.log(this.letter)
       if(this.letter){
         /*
            注意：
            element 变量是一个数组 加上[0]就可以了这里其实js Dom的知识点
            scrollToElement() 接收的必须是Dom选择器
         */
         const element = this.$refs[this.letter][0]
        //  console.log(element)
          this.scroll.scrollToElement(element)
       }
    }
  },
  mounted() {
    this.scroll = new BScroll(this.$refs.wrapper,{
      // better-scroll 默认会阻止浏览器的原生 click 事件。当设置为 true，better-scroll 会派发一个 click 事件
      click: true
    })
  },
  computed: {
    ...mapState({
      currentCity: 'city'
    })
  }
}
</script>

<style lang="less" scoped>
  @import "~styles/varlibase.less";
  .border-topbottom{
    &::before{
      border-color: #ccc;
    }
    &::after{
      border-color: #ccc;
    }
  }
  .border-bottom{
    &::before{
      border-color: #ccc;
    }
  }
  .list{
    overflow: hidden;
    position: absolute;
    top: 1.58rem;
    left: 0;
    right: 0;
    bottom: 0;
    .title{
      line-height: .54rem;
      background-color: #eee;
      padding-left: .2rem;
      color: #666;
      font-size: .26rem;
    }
    .button-list{
      padding: .1rem .6rem .1rem .1rem;
      overflow: hidden;
      .button-wrapper{
        width: 33.33%;
        float: left;
        .button{
          padding: .1rem 0;
          text-align: center;
          margin: .1rem;
          border: .02rem solid #ccc;
          border-radius:  .06rem;
        }
      }
    }
    .item-list{
      .item{
        line-height: .76rem;
        padding-left: .2rem;
      }
    }
}
</style>
